Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WW-5408 add option to not fallback to empty namespace when unresolved #912

Conversation

jefferyxhy
Copy link
Contributor

WW-5408

Reason
There is a case we want to enhance to improve security for struts url mapping: Be able to prevent arbitrary namespace fallback to empty (root) namespace when not match

 

Changes/ Solution

  • introduce new Struts constant STRUTS_DISABLE_EMPTY_NAMESPACE_FALLBACK [struts.disableActionConfigFallbackToEmptyNamespace] to disable fallback to empty namespace when not match
  • default as null which means does not disable (Need to manually set as true in confluence struts.xml)

 

Result & Impact

  • By default struts.disableActionConfigFallbackToEmptyNamespace is null, no difference.
  • Set struts.disableActionConfigFallbackToEmptyNamespace as true, not matched namepsace WILL NOT fallback to empty namespace anymore, and struts will threw ConfigurationException, it relies on the application to handle this exception.

@jefferyxhy jefferyxhy marked this pull request as ready for review April 9, 2024 05:42
@@ -230,6 +230,8 @@ public final class StrutsConstants {
public static final String STRUTS_XWORKCONVERTER = "struts.xworkConverter";

public static final String STRUTS_ALWAYS_SELECT_FULL_NAMESPACE = "struts.mapper.alwaysSelectFullNamespace";
/** Disable fallback to empty namespace when request namespace didn't match any in action configuration */
public static final String STRUTS_DISABLE_EMPTY_NAMESPACE_FALLBACK = "struts.disableActionConfigFallbackToEmptyNamespace";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukaszlenart Any thoughts on what the best name for this constant might be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to have struts.actionConfig.fallbackToEmptyNamespace with default value set to true instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me - @jefferyxhy could you please update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kusalk @lukaszlenart updated. Thanks

… struts.actionConfig.fallbackToEmptyNamespace
Copy link
Member

@lukaszlenart lukaszlenart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, two minor issues

@@ -583,11 +590,10 @@ public ActionConfig getActionConfig(String namespace, String name) {
}

// fail over to empty namespace
if (config == null && StringUtils.isNotBlank(namespace)) {
if (config == null && StringUtils.isNotBlank(namespace) && ("/".equals(namespace) || fallbackToEmptyNamespace)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract this logic into a private method to name it, something like

if (config == null && shouldFallbackToEmptyNamespaces(namespace)) {
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks

@@ -459,9 +460,12 @@ protected synchronized RuntimeConfiguration buildRuntimeConfiguration() throws C
boolean appendNamedParameters = Boolean.parseBoolean(
container.getInstance(String.class, StrutsConstants.STRUTS_MATCHER_APPEND_NAMED_PARAMETERS)
);
boolean fallbackToEmptyNamespace = Boolean.parseBoolean(
Optional.ofNullable(container.getInstance(String.class, StrutsConstants.STRUTS_ACTION_CONFIG_FALLBACK_TO_EMPTY_NAMESPACE)).orElse("true")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this fallback I would create a new entry into default.properties with value set to true and document it - this will help others understand the change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jefferyxhy and I just discussed this one and one of the drawbacks of putting it in default.properties is that it isn't read by unit tests and causes a bunch of test failures, as the unit tests will default to false. To get around this we could additionally add the constant to StrutsDefaultConfigurationProvider as well as default.properties.

I'm personally not too fussed. In the past I've deliberately made constants default to false to try sidestep this issue. Let us know know what you would prefer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, yet default.properties is used as a documentation by users so I would define the constant there with short description and keep fallback to true as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks

@kusalk kusalk merged commit 1562e66 into apache:master Apr 12, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants